package com.hnkywl.sts.dao.htgl;

import com.hnkywl.sts.entity.htgl.Htmx;
import com.hnkywl.sts.entity.htgl.Xsht;
import com.hnkywl.sts.webservice.bg.request.ContractDetail;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.Type;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 灵韵
 * Date: 13-8-10
 * Time: 下午4:57
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class HtmxDao extends SimpleHibernateTemplate<Htmx, Long> {
    public Page<Htmx> findCgHtmxList(Long cghtId, int pageNo, int pageSize) {
        Page<Map> page = new Page<Map>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("id");
        Criteria criteria = getSession().createCriteria(Htmx.class)
                .createAlias("wzgg", "wzgg", CriteriaSpecification.LEFT_JOIN)
                .createAlias("cd", "cd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("jzfs", "jzfs", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                                .add(Property.forName("wzgg.id").as("wzgg.id"))
                                .add(Property.forName("wzgg.code").as("wzgg.code"))
                                .add(Property.forName("wzgg.oldcode").as("wzgg.oldcode"))
                                .add(Property.forName("wzgg.name").as("wzgg.name"))
                                .add(Projections.sqlProjection("wzgg1_.cz||' '||wzgg1_.ggxh as \"wzgg.ggxh\"", new String[]{"wzgg.ggxh"}, new Type[]{Hibernate.STRING}))
                                .add(Property.forName("wzgg.dw").as("wzgg.dw"))
                                .add(Property.forName("cd.id").as("cd.id"))
                                .add(Property.forName("cd.z").as("cd.z"))
                                .add(Property.forName("ck").as("ck"))
                                .add(Property.forName("sbsl").as("sbsl"))
                                .add(Property.forName("jzfs.id").as("jzfs.id"))
                                .add(Property.forName("jzfs.z").as("jzfs.z"))
                                .add(Property.forName("ph").as("ph"))
                                .add(Property.forName("jthsjsl").as("jthsjsl"))
                                .add(Property.forName("hsj").as("hsj"))
                                .add(Property.forName("bhsj").as("bhsj"))
                                .add(Property.forName("hsje").as("hsje"))
                                .add(Property.forName("bz").as("bz"))
                ).add(Restrictions.eq("ht.id", cghtId))
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        return this.findByCriteria(page, criteria);
    }

    /**
     * 更新保供合同明细
     *
     * @param list
     */
    public void updateBghtmx(List<ContractDetail> list) {
        String sql = "update t_biz_htmx mx set mx.sbsl=?,mx.bhsj=?,mx.hsj=?," +
                "mx.ph=?,mx.jthsjsl=?,mx.bz=?,mx.zt=?,mx.last_updated=? where mx.contractdetailid=?";
        Date today = new Date();
        for (int i = 0; i < list.size(); i++) {
            ContractDetail d = list.get(i);
            executeSQL(sql, d.getQty(), d.getPrice(), d.getTaxPrice(), d.getBrand(), d.getDeliveryDate(), d.getDesc(), d.getZt(), today, d.getContractDetailId());
            if ((i + 1) % 20 == 0)
                getSession().flush();
        }
    }

    public Htmx findHtmxByHtAndGg(String xsxt_xsdh, Long htId) {
        String sql = "select * from t_biz_htmx where deleted = 0 and xsxt_xsdh = ? and ht_id = ?";
        Query query = createSQLQuery(sql, xsxt_xsdh.trim(), htId).addEntity(Htmx.class);
        return (Htmx) query.list().get(0);
    }
}
